<template>
  <div class="bg-content">
    <div class="main">
      <div class="detailtitle">{{ detail.title }}</div>

      <div class="head">
        <div class="look">阅读量 {{ detail.watchNum }}</div>
        <div class="dian">点赞量 {{ detail.clickNum }}</div>
      </div>
      <div class="middile" v-html="detail.content"></div>
      <div class="foot">
        <div v-if="show" class="dianimg">
          <img src="../../../../assets/img/main/dian1.png" alt="" />
        </div>
        <div v-else class="dianimg" @click="dianzan()">
          <img src="../../../../assets/img/main/dian.png" alt="" />
        </div>
        <div class="diannum">{{ detail.clickNum }}</div>
      </div>
    </div>
  </div>
</template>
<script>
// import { orderDetailsApi, cancelOrderApi } from "@/service/api.js";
import { jiankangdetailApi } from "@/service/api.js";
import { dianzanApi } from "@/service/api.js";
import { loginByWxOpenidApi } from "@/service/api.js";

import { Dialog } from "vant";
import { Tab, Tabs, Popup } from "vant";
import { Toast } from "vant";
export default {
  name: "index",
  data() {
    return {
      apptoken: "",
      token: "",
      newsId: "",
      pageNumber: "",
      show: "",
      detail: {}
    };
  },
  created() {
    this.newsId = JSON.parse(localStorage.getItem("newsId"));
    this.pageNumber = JSON.parse(localStorage.getItem("pageNumber"));
    this.apptoken = JSON.parse(localStorage.getItem("apptoken"));
    localStorage.setItem("apptoken1", this.apptoken);
    this.token = localStorage.getItem("token");

    this.getdetail();
  },
  methods: {
    getdetail() {
      jiankangdetailApi({
        apptoken: this.apptoken,
        token: this.token,
        newsId: this.newsId,
        pageNumber: this.pageNumber
      }).then(res => {
        console.log(res);
        if (res.code == 4000) {
          this.detail = res.data;
          this.show = res.data.isPraise;
        } else {
          Toast(res.msg);
        }
      });
    },
    dianzan() {
      if (this.token) {
        dianzanApi({
          apptoken: this.apptoken,
          token: this.token,
          newsId: this.newsId,
          pageNumber: this.pageNumber
        }).then(res => {
          console.log(res);
          if (res.code == 4000) {
            // Toast(res.msg);
            this.show = true;
            this.detail.clickNum++;
          } else if (res.code == 4002) {
            window.localStorage.removeItem("token");
            Dialog.confirm({
              title: "提示",
              message: "需前往登录页面完成登录"
            })
              .then(() => {
                this.$router.push({
                  name: "login"
                });
                // on confirm
              })
              .catch(() => {
                // on cancel
              });
          } else {
            Toast(res.msg);
          }
        });
      } else {
        Dialog.confirm({
          title: "提示",
          message: "需前往登录页面完成登录"
        })
          .then(() => {
            this.$router.push({
              name: "login"
            });
            // on confirm
          })
          .catch(() => {
            // on cancel
          });
      }
    }
  }
};
</script>
<style lang="scss">
.middile {
  width: 100%;
  margin: 0 auto;
  margin-top: 0.5rem;
  font-size: 14px;
  img {
    width: 100%;
    height: 200px;
    margin-bottom: 20px;
    margin-top: 20px;
  }
  p {
    line-height: 24px;
    letter-spacing: 1px;
    font-size: 14px;
  }
}
</style>
<style lang="scss" scoped>
.bg-content {
  width: 100%;
  padding-bottom: 80px;
  padding-top: 1.2rem;
  // overflow: hidden;
  .main {
    width: 90%;
    margin: 0 auto;
    margin-top: 0.2rem;
    padding-bottom: 2rem;
    .detailtitle {
      font-size: 0.5rem;
      text-align: center;
    }
    // overflow: hidden;
    .head {
      margin-top: 0.2rem;
      display: flex;
      justify-content: space-between;
      .look {
        font-size: 0.35rem;
        color: #999;
      }
      .dian {
        font-size: 0.35rem;
        color: #999;
      }
    }
  }
  .foot {
    display: flex;
    margin-left: 78%;
    line-height: 0.5rem;
    .dianimg {
      width: 0.5rem;
      height: 0.5rem;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .diannum {
      margin-left: 0.1rem;
      font-size: 0.4rem;
      color: #333;
    }
  }
}
</style>
